package test.src.awt_swing;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import java.awt.*;

public class ExtendsDefaultCellTreeRenderer {
    JFrame jFrame = new JFrame("根据结点类型定义图标");
    JTree jTree;

    //定义几个初始结点
    DataNode rootData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\root.gif"), "数据库导航");
    DefaultMutableTreeNode root = new DefaultMutableTreeNode(rootData);

    DataNode salaryDbData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\database.gif"), "公司工资数据库");
    DefaultMutableTreeNode salaryDb = new DefaultMutableTreeNode(salaryDbData);

    DataNode customerDbData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\database.gif"), "公司客户数据库");
    DefaultMutableTreeNode customerDb = new DefaultMutableTreeNode(customerDbData);

    DataNode employeeData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\table.gif"), "员工表");
    DefaultMutableTreeNode employee = new DefaultMutableTreeNode(employeeData);

    DataNode attendData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\table.gif"), "考勤表");
    DefaultMutableTreeNode attend = new DefaultMutableTreeNode(attendData);

    DataNode concatData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\table.gif"), "联系方式表");
    DefaultMutableTreeNode concat = new DefaultMutableTreeNode(concatData);

    DataNode idData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\index.gif"), "员工id");
    DefaultMutableTreeNode id = new DefaultMutableTreeNode(idData);

    DataNode nameData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\column.gif"), "姓名");
    DefaultMutableTreeNode name = new DefaultMutableTreeNode(nameData);

    DataNode genderData = new DataNode(new ImageIcon("test\\src\\awt_swing\\img\\tree\\column.gif"), "性别");
    DefaultMutableTreeNode gender = new DefaultMutableTreeNode(genderData);

    private void init() {
        root.add(salaryDb);
        root.add(customerDb);
        salaryDb.add(employee);
        salaryDb.add(attend);
        customerDb.add(concat);
        concat.add(id);
        concat.add(name);
        concat.add(gender);

        jTree = new JTree(root);
        jTree.setCellRenderer(new MyTreeRenderer());
        jFrame.add(new JScrollPane(jTree));
        jFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jFrame.setBounds(400, 400, 300, 400);
        jFrame.pack();
        jFrame.setVisible(true);
    }

    class MyTreeRenderer extends DefaultTreeCellRenderer {
        @Override
        public Component getTreeCellRendererComponent(JTree tree, Object value, boolean sel, boolean expanded, boolean leaf, int row, boolean hasFocus) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
            DataNode data = (DataNode) node.getUserObject();

            this.setIcon(data.icon);
            this.setText(data.name);

            return this;
        }
    }

    public static void main(String[] args) {
        new ExtendsDefaultCellTreeRenderer().init();
    }
}

class DataNode {
    ImageIcon icon;
    String name;

    DataNode(ImageIcon icon, String name) {
        this.icon = icon;
        this.name = name;
    }
}
